﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;

namespace MockPrjTeam2.Pages.Views
{
    public partial class Premises : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void show()
        {
            var currentIndex = Grid.CurrentPageIndex;

            var result = PremisesBiz.Sp_Premises_GetAll();

            Grid.DataSource = result;
            Grid.CurrentPageIndex = 0;
            Grid.DataBind();

            if (Grid.PageCount > 0 && currentIndex > Grid.PageCount - 1)
            {
                currentIndex = Grid.PageCount - 1;
            }

            Grid.CurrentPageIndex = currentIndex;
            Grid.DataBind();
            //Grid.CurrentPageIndex = 0;
            if (Grid.PageCount <= 1)
            {
                Grid.PagerStyle.Visible = false;
            }
        }

        protected void LinkButton11_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.Sp_Premises_GetAll();
            Grid.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Grid.DataSource=PremisesBiz.StartWith("0-9");
            Grid.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("a-e");
            Grid.DataBind();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("f-j");
            Grid.DataBind();
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("k-n");
            Grid.DataBind();
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("o-r");
            Grid.DataBind();
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("s-v");
            Grid.DataBind();
        }

        protected void LinkButton8_Click(object sender, EventArgs e)
        {
            Grid.DataSource = PremisesBiz.StartWith("w-z");
            Grid.DataBind();
        }

        protected void LinkButton9_Click(object sender, EventArgs e)
        {
            TextBox txtma;
            foreach (DataGridItem Items in this.Grid.Items)
            {
                CheckBox chk = (CheckBox)Items.FindControl("chkselect");
                txtma = (TextBox)Items.FindControl("txtid");
                if (chk.Checked == true)
                {
                    PremisesBiz.Premises_delete(txtma.Text);
                }
            }
            Grid.CurrentPageIndex = 0;
            show();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Forms/Premises.aspx");
        }

        protected void chkselectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkselectAll = (CheckBox)sender;
            foreach (DataGridItem Items in this.Grid.Items)
            {
                CheckBox chk = (CheckBox)Items.FindControl("chkselect");
                chk.Checked = chkselectAll.Checked;
            }
        }

        protected void Grid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strA = e.CommandArgument.ToString();
            if (e.CommandName.ToString().Equals("Sua"))
            {
                //insert = false;
                //List<Entity.ProgrammesInfor> lst = new List<Entity.ProgrammesInfor>();
                //lst = ProgrammesBIZ.Programme_Getby_Id(strA);
                //if (lst.Count > 0)
                //{
                //    txtProgrammeName.Text = lst[0].ProgrammeName;
                //    txtDescription.Text = lst[0].Description;
                //    txtContact.Text = lst[0].Contact;
                //}
                //pnshow.Visible = false;
                //pnupdate.Visible = true;
                Response.Redirect("~/Pages/Forms/Premises.aspx?ID=" + strA + "&insert=a");
            }
            else
            {
                PremisesBiz.Premises_delete(strA);
                show();
            }
        }

        protected void Grid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            Grid.CurrentPageIndex = e.NewPageIndex;
            show();
        }
    }
}